package sysytmRepo

import (
	"gorm.io/gorm"
	"ruoyi/domain/entity/common"
	"ruoyi/domain/entity/response"
	"ruoyi/domain/entity/system"
	"ruoyi/domain/repository"
	"ruoyi/utils"
)

type SysDictTypeRepo struct {
	db *gorm.DB
}

func SysDictTypeRepository(db *gorm.DB) *SysDictTypeRepo {
	return &SysDictTypeRepo{db}
}

var _ repository.SysDictTypeRepository = &SysDictTypeRepo{}

// 分页查询
func (d *SysDictTypeRepo) SelectSysDictTypeList(params common.SearchTableDataParam, isPage bool) common.TableDataInfo {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	sysDictType := params.Other.(system.SysDictType)
	offset := (pageNum - 1) * pageSize
	var total int64
	var rows []system.SysDictType

	var db = d.db.Model(&rows)

	var dictName = sysDictType.DictName
	if dictName != "" {
		db.Where("dict_name like ?", "%"+dictName+"%")
	}

	var status = sysDictType.Status
	if status != "" {
		db.Where("status =", status)
	}

	var dictType = sysDictType.DictType
	if dictType != "" {
		db.Where("dict_type like ?", dictType)
	}

	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	if beginTime != "" {
		//Loc, _ := time.LoadLocation("Asia/Shanghai")
		//startTime1, _ := time.ParseInLocation(constants.DateFormat, beginTime, Loc)
		//endTime = endTime + " 23:59:59"
		//endTime1, _ := time.ParseInLocation(constants.TimeFormat, endTime, Loc)
		startTime1, endTime1 := utils.GetBeginAndEndTime(beginTime, endTime)
		db.Where("create_time >= ?", startTime1)
		db.Where("create_time <= ?", endTime1)
	}
	if err := db.Count(&total).Error; err != nil {
		return common.Fail()
	}
	if isPage {
		if err := db.Limit(pageSize).Offset(offset).Find(&rows).Error; err != nil {
			return common.Fail()
		}
	} else {
		if err := db.Find(&rows).Error; err != nil {
			return common.Fail()
		}
	}

	if rows == nil {
		return common.Fail()
	} else {
		return common.Success(rows, total)
	}
}

func (d *SysDictTypeRepo) FindTypeDictById(dictId string) system.SysDictType {
	var dictType system.SysDictType
	err := d.db.Where("dict_id = ?", dictId).First(&dictType).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}
	return dictType
}

func (d *SysDictTypeRepo) SaveType(dictType system.SysDictType) response.Response {
	err := d.db.Model(&system.SysDictType{}).Create(&dictType).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (d *SysDictTypeRepo) UploadType(dictType system.SysDictType) response.Response {
	err := d.db.Updates(&dictType).Error
	if err != nil {
		return response.ReturnFailMsg(err.Error())
	}
	return response.ReturnSuccess("操作成功")
}

func (d *SysDictTypeRepo) DeleteDataType(dictIds string) response.Response {
	ids := utils.Split(dictIds)
	for i := 0; i < len(ids); i++ {
		id := ids[i]
		err := d.db.Where("dict_id = ?", id).Delete(&system.SysDictType{}).Error
		if err != nil {
			return response.ReturnFailMsg(err.Error())
		}
	}
	return response.ReturnSuccess("操作成功")
}

func (d *SysDictTypeRepo) RefreshCache() response.Response {
	/*删除缓存*/
	/*重新赋值初始化参数*/
	return response.ReturnSuccess("操作成功")
}

func (d *SysDictTypeRepo) GetOptionSelect() response.Response {
	var sysDictType []system.SysDictType
	err := d.db.Find(&sysDictType).Error
	if err != nil {
		panic(response.ReturnFailMsg(err.Error()))
	}
	return response.ReturnSuccess(sysDictType)
}
